package io.zhengsh.juc.singleton;

/**
 * 单例模式: 懒汉式，在首次使用的时候去初始化
 */
public class Singleton02 {

    private static volatile Singleton02 INSTANCE;

    private Singleton02() {
    }

    public static Singleton02 getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton02.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton02();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        Singleton02 singleton01 = Singleton02.getInstance();
        Singleton02 singleton02 = Singleton02.getInstance();

        System.out.println(singleton01 == singleton02);
        // true
    }
}
